package com.xscha.tableforge.controller;

import com.xscha.soyue.common.core.domain.R;
import com.xscha.soyue.common.core.utils.BeanMapperUtils;
import com.xscha.tableforge.model.dto.FieldMetaDto;
import com.xscha.tableforge.model.entity.FieldMeta;
import com.xscha.tableforge.model.vo.FieldMetaVo;
import com.xscha.tableforge.model.vo.PageReq;
import com.xscha.tableforge.model.vo.PageResult;
import com.xscha.tableforge.service.FieldMetaService;
import com.xscha.tableforge.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/fields")
public class FieldMetaController {

    private final FieldMetaService fieldMetaService;

    @Autowired
    public FieldMetaController(FieldMetaService fieldMetaService) {
        this.fieldMetaService = fieldMetaService;
    }

    @GetMapping("/table/{tableId}")
    public R<List<FieldMetaVo>> getFieldsByTableId(@PathVariable String tableId) {
        return R.ok(BeanMapperUtils.mapAsList(fieldMetaService.findAllByTableId(tableId), FieldMetaVo.class));
    }
    
    @PostMapping("/table/{tableId}/page")
    public R<PageResult<FieldMetaVo>> getFieldsByTableIdPage(
            @PathVariable String tableId,
            @RequestBody PageReq pageRequest) {
        PageResult<FieldMeta> result = fieldMetaService.findPageByTableId(tableId, pageRequest);
		List<FieldMetaVo> fieldMetaVos = BeanMapperUtils.mapAsList(result.getRecords(), FieldMetaVo.class);
		return R.ok(PageResult.of(fieldMetaVos, result.getTotal(), result.getPageNum(), result.getPageSize()));
    }

    @GetMapping("/{id}")
    public R<FieldMetaVo> getFieldById(@PathVariable String id) {
        return R.ok(BeanMapperUtils.map(fieldMetaService.findById(id), FieldMetaVo.class));
    }

    @PostMapping
    public R<FieldMetaVo> createField(@RequestBody FieldMetaDto fieldMetaDto) {
        try {
            FieldMeta createdField = fieldMetaService.createFieldMeta(fieldMetaDto, SecurityUtils.getUserId());
            return R.ok(BeanMapperUtils.map(createdField, FieldMetaVo.class));
        } catch (Exception e) {
            return R.fail(e.getMessage());
        }
    }

    @PutMapping("/{id}")
    public R<FieldMetaVo> updateField(@PathVariable String id, 
                                     @RequestBody FieldMetaDto fieldMetaDto) {
        if (!id.equals(fieldMetaDto.getId())) {
            return R.fail("ID不匹配");
        }
        
        try {
            FieldMeta updatedField = fieldMetaService.updateFieldMeta(id, fieldMetaDto, SecurityUtils.getUserId());
            return R.ok(BeanMapperUtils.map(updatedField, FieldMetaVo.class));
        } catch (Exception e) {
            return R.fail(e.getMessage());
        }
    }

    @DeleteMapping("/{id}")
    public R<Void> deleteField(@PathVariable String id) {
        try {
            fieldMetaService.deleteFieldMeta(id, SecurityUtils.getUserId());
            return R.ok();
        } catch (Exception e) {
            return R.fail(e.getMessage());
        }
    }
}